<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>阳光成单系统</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">
<link rel="stylesheet" href="./css/x-admin.css" media="all">
<style type="text/css">
#mytable {
	table-layout: fixed;
	width: 98% border:0px;
	margin: 0px;
}

#mytable tr td {
	text-overflow: ellipsis; /* for IE */
	-moz-text-overflow: ellipsis; /* for Firefox,mozilla */
	overflow: hidden;
	white-space: nowrap;
	border: 1px solid;
	text-align: left
}
</style>
</head>
<body>
	<div class="x-nav">
		<span class="layui-breadcrumb"> <a><cite>新闻首页</cite></a> <a><cite>新闻管理</cite></a>
			<a><cite>新闻详情</cite></a>
		</span> <a class="layui-btn layui-btn-small"
			style="line-height: 1.6em; margin-top: 3px; float: right"
			href="javascript:location.replace(location.href);" title="刷新"><i
			class="layui-icon" style="line-height: 30px">ဂ</i></a>
	</div>
	<div class="x-body">
		<form id="form_news" class="layui-form x-center"
			onsubmit="return false" style="width: 70%">
			<div class="layui-form-pane" style="margin-top: 15px;">
				<div class="layui-form-item">
					<label class="layui-form-label" style="width: 100px">所属分类</label>
					<div class="layui-input-inline"
						style="width: 120px; text-align: left">
						<select name="newsCategoryId">
							<option value="">请选择分类</option>
							<option value="1">行业新闻</option>
							<option value="2">饮品百科</option>
							<option value="3">公司新闻</option>
							<option value="4">时事热点</option>
						</select>
					</div>
					<div class="layui-input-inline" style="width: 120px">
						<input type="text" name="newsTitle" placeholder="根据标题"
							autocomplete="off" class="layui-input">
					</div>
					<div class="layui-input-inline" style="width: 120px">
						<input type="text" name="nwesContent" placeholder="根据内容"
							autocomplete="off" class="layui-input">
					</div>
					<div class="layui-input-inline" style="width: 80px">
						<button class="layui-btn"
							onclick="admin_add('添加用户','newsdetails-add.html','600','500')">
							<i class="layui-icon">&#xe608;</i>添加
						</button>

					</div>
					<div class="layui-input-inline" style="width: 80px">
						<button class="layui-btn" onclick="mypage()"
							lay-filter="add">查询</button>
					</div>
				</div>
			</div>
		</form>
		<button class="layui-btn layui-btn-danger" onclick="delAll()">
			<i class="layui-icon">&#xe640;</i>批量删除
		</button>
		<span class="x-right" style="line-height: 40px">共有数据：88 条</span>
		</xblock>
		<table class="layui-table" id="mytable">
			<thead>
				<tr>
					<th><input type="checkbox" onclick="selAll(this)" name=""
						value=""></th>
					<th>ID</th>
					<th>排序</th>
					<th>标题</th>
					<th>内容</th>
					<th>分类名</th>
					<th>操作</th>
				</tr>
			</thead>
			<tbody id="table_news_details">
				<!-- 新闻详情表格 -->

			</tbody>
			
		</table>
		<div id="mypage"></div>
	</div>
	<script src="./lib/layui/layui.js" charset="utf-8"></script>
	<script src="./js/x-layui.js" charset="utf-8"></script>
	<script>
			var $=null;
			var layer=null;
			var laypage=null;
            layui.use(['element','layer','form','laypage'], function(){
                $ = layui.jquery;//jquery      
             	layer = layui.layer;//弹出层
             	laypage = layui.laypage;
				
             	$(function(){//页面加载事件
					mypage();
				});
            })
            function mypage(){
            	$.post("../adminNewsCategory?reqName=getAllNewsDetails",$("#form_news").serialize(),function(res){
            		laypage.render({
         			    elem: 'mypage', //注意，这里的 test1 是 ID，不用加 # 号
         			    count: parseInt(res),//数据总数，从服务端得到
         			    limit: 4,
         			   	jump: function(obj, first){
         				    //obj包含了当前分页的所有参数，比如：
         				    console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
         				    console.log(obj.limit); //得到每页显示的条数
         				    
         				   selNewsDetails(obj.curr, obj.limit);
         				  }
         		  	});
            	},"json");
            	 
            }
			function selNewsDetails(page, size){ <!-- 查询新闻详情的方法 -->
				$("#table_news_details").html("");
				$.post("../adminNewsCategory?reqName=getNewsDetails",
						$.param({"page":page})+'&'+$.param({"size":size})+'&'+$("#form_news").serialize()
					,function(res){
						for (var i = 0; i < res.length; i++) {//遍历数组
							$("#table_news_details").append(`
								<tr>
			                        <td>
			                            <input type="checkbox" value="1" name="">
			                        </td>
			                        <td>${res[i].newsId}</td>
			                        <td>
			                        	${i+1}
			                        </td>
			                        <td>
			                          	 ${res[i].newsTitle}
			                        </td>
			                        <td>
		                          	 ${res[i].nwesContent}
		                        	</td>
		                        	<td>
		                          	 ${res[i].newsCategoryName}
		                        	</td>
			                        <td class="td-manage">
			                            <a title="编辑" href="javascript:;" onclick="cate_edit('编辑','newsdetails-edit.html',${res[i].newsId},'','510')"
			                            class="ml-5" style="text-decoration:none">
			                                <i class="layui-icon">&#xe642;</i>
			                            </a>
			                            <a title="删除" href="javascript:;" onclick="cate_del(this, ${res[i].newsId})" 
			                            style="text-decoration:none">
			                                <i class="layui-icon">&#xe640;</i>
			                            </a>
			                        </td>
		                  	  </tr>
								
								`);
					}
				},"json");
			}
          //设置全选全不选
            function selAll(obj){
          	  var checked=$(obj).prop("checked");//得到表头中的复选框是否被选中
          	  $("#table_news_details input:checkbox").prop("checked",checked); 
            }
              
            //批量删除提交
             function delAll () {
            	var chks=$("#table_news_details input:checkbox:checked");//获得所有被选中的复选框 产生的是个数组
                if (chks.length==0) {
					alert("勾选有效的复选框");
					return false;
				}
            	layer.confirm('确认要删除吗？',function(index){
            		var ids=[];
            		for (var i = 0; i < chks.length; i++) {
						var abc=$(chks[i]).parent().next().html();
						ids.push(abc);
					}
            		$.post("../adminNewsCategory?reqName=delNewsDetailsByIds",{
            			ids: ids.join(",")//将数组变成字符串
            		},function(res){
            			if (res.success) {
            				 //捉到所有被选中的，发异步进行删除
                            layer.msg('删除成功', {icon: 1});
                            mypage();
						} else {
							//捉到所有被选中的，发异步进行删除
		                    layer.msg('删除失败', {icon: 1});
						}
            		},"json");
                    
                });
             }

             //-编辑
            function cate_edit (title,url,id,w,h) {
            	 
            	sessionStorage.setItem("news", id); // 把id存到浏览器会话中
                x_admin_show(title,url,w,h); 
            }
            /*添加*/
            function admin_add(title,url,w,h){
                x_admin_show(title,url,w,h);
            }
            /*-删除*/
            function cate_del(obj,id){
            	
                layer.confirm('确认要删除吗？',function(index){
                    //发异步删除数据
                    $.post("../adminNewsCategory?reqName=delNewsDetails",{
                    	newsId:id
                    },function(res){//res是返回的数据
                    	if (res.success) {//删除成功
                    		layer.msg(res.message,{icon:1,time:2000});
                    		$(obj).parents("tr").remove();
                    		myPager();
						} else {//删除失败
							layer.msg(res.message,{icon:1,time:2000});
						}
                    },"json")
                    
                    
                });
            }
            </script>

</body>
</html>